<?php

class TutorialsController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout = '/layouts/triple_sidebar';
	public $_id;
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','create','update','admin','delete', 'DeleteFiles'),
				'roles'=>array('teacher'),
				'users'=>array('*'),
			),
			/*array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),*/
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Tutorials;
		$model1 = new Files;
		$model2 = new FileAssignment;
		
		$model11 = new Files;
		$model12 = new Files;
		$model13 = new Files;
		$model21 = new FileAssignment;
		$model22 = new FileAssignment;
		$model23 = new FileAssignment;
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Tutorials']))
		{
			$model->attributes=$_POST['Tutorials'];
			$model1->attributes=$_POST['Files'];
			$model->DateCreated = date_create()->format('Y-m-d H:i:s');
			//$model1->location = $model->location;
			$model->location = 'Youtube';
			$temp = $model->DateCreated;
			$temp2 = $model1->location;
			$file = CUploadedFile::getInstance($model, 'document');
			$file1 = CUploadedFile::getInstance($model, 'document1');
			$file2 = CUploadedFile::getInstance($model, 'document2');
			
			
			if($file === null && $file1 === null && $file2 === null){
					$model->save();
					$model1->save();
					$model2->tutorial_id = Tutorials::model()->findByAttributes(array('DateCreated'=>$temp))->id; 
					$model2->file_id = Files::model()->findByAttributes(array('location'=>$temp2))->id;
					$model2->save();
					$this->redirect(array('tutorials/index'));
				}
			
			if($file1 !== null){
					$filename1 = "{$file1}"; 
					$location1 = Yii::app()->basePath.'/modules/teacher/files/tutorials/'.$filename1;
				}
				
			if($file2 !== null){
					$filename2 = "{$file2}"; 
					$location2 = Yii::app()->basePath.'/modules/teacher/files/tutorials/'.$filename2;
				}
			
			$filename = "{$file}"; 
			$location0 = Yii::app()->basePath.'/modules/teacher/files/tutorials/'.$filename;
			
			if($model->save()){
				$id0 = Tutorials::model()->findByAttributes(array('title'=>$model->title))->id;
				if($file !== null){
					$file->saveAs($location0);
					$model11->location = $location0;
					$model11->save();
					$model21->tutorial_id = $id0;
					$model21->file_id = Files::model()->findByAttributes(array('location'=>$location0))->id;
					$model21->save();
				}
				if($file1 !== null){
					$file1->saveAs($location1);
					$model12->location = $location1;
					$model12->save();
					$model22->tutorial_id = $id0;
					$model22->file_id = Files::model()->findByAttributes(array('location'=>$location1))->id;
					$model22->save();
					}
				if($file2 !== null){
					$file2->saveAs($location2);
					$model13->location = $location2;
					$model13->save();
					$model23->tutorial_id = $id0;
					$model23->file_id = Files::model()->findByAttributes(array('location'=>$location2))->id;
					$model23->save();
					
					}
			
					
					
			}
					
				$this->redirect(array('tutorials/index'));
				
		}

		$this->render('create',array(
			'model'=>$model,
			'model1'=>$model1,
			'model2'=>$model21,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		$id1 = FileAssignment::model()->findByAttributes(array('tutorial_id'=>$id))->file_id;
		$model1 = Files::model()->findByPk($id1);
		$model2 = FileAssignment::model()->findAllByAttributes(array('tutorial_id'=>$id));
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		
		$model11 = new Files;
		$model12 = new Files;
		$model13 = new Files;
		$model21 = new FileAssignment;
		$model22 = new FileAssignment;
		$model23 = new FileAssignment;

		if(isset($_POST['Tutorials']))
		{
			$model->attributes=$_POST['Tutorials'];
			
			$file = CUploadedFile::getInstance($model, 'document');
			$file1 = CUploadedFile::getInstance($model, 'document1');
			$file2 = CUploadedFile::getInstance($model, 'document2');
			
			if($file1 !== null){
					$filename1 = "{$file1}"; 
					$location1 = Yii::app()->basePath.'/modules/teacher/files/tutorials/'.$filename1;
				}
				
			if($file2 !== null){
					$filename2 = "{$file2}"; 
					$location2 = Yii::app()->basePath.'/modules/teacher/files/tutorials/'.$filename2;
				}
			
			$filename = "{$file}"; 
			$location0 = Yii::app()->basePath.'/modules/teacher/files/tutorials/'.$filename;
			
			if($model->save()){
				$id0 = $id;
				if($file !== null){
					$file->saveAs($location0);
					$model11->location = $location0;
					$model11->save();
					$model21->tutorial_id = $id0;
					$model21->file_id = Files::model()->findByAttributes(array('location'=>$location0))->id;
					$model21->save();
				}
				if($file1 !== null){
					$file1->saveAs($location1);
					$model12->location = $location1;
					$model12->save();
					$model22->tutorial_id = $id0;
					$model22->file_id = Files::model()->findByAttributes(array('location'=>$location1))->id;
					$model22->save();
					}
				if($file2 !== null){
					$file2->saveAs($location2);
					$model13->location = $location2;
					$model13->save();
					$model23->tutorial_id = $id0;
					$model23->file_id = Files::model()->findByAttributes(array('location'=>$location2))->id;
					$model23->save();
					}
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('update',array(
			'model'=>$model,
			'model1'=>$model1,
			'model2'=>$model2,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Tutorials');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Tutorials('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Tutorials']))
			$model->attributes=$_GET['Tutorials'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Tutorials the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Tutorials::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Tutorials $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='tutorials-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function actionDeleteFiles($id, $id1){
			$location = Files::model()->findByPk($id1);
			Files::model()->deleteAllByAttributes(array('id'=>$id1));
			FileAssignment::model()->deleteAllByAttributes(array('file_id'=>$id1));
			unlink($location->location);
			$this->redirect(array('update', 'id'=>$id));
		}
}
